package game

import (
	"server/game/pkg/rank"
	"server/share/config"
	"server/share/define"
	"time"

	"github.com/hwcer/cosgo/times"
	"github.com/hwcer/cosgo/utils"
	"github.com/hwcer/logger"
)

func init() {
	rank.Register(define.RankNameDailyBoss, 100, 1, rank.SortTypeDesc, &rankDaily{})
	rank.Register(define.RankNameArena, 1000, 1, rank.SortTypeDesc, &rankArena{})
}

type rankDaily struct{}

func (p *rankDaily) Truce() int64 {
	return 0
}
func (p *rankDaily) Cycle(skip int64) int64 {
	s, _ := times.Sign(int(skip))
	return int64(s)
}

func (p *rankDaily) Submit(b *rank.Bucket, cycle int64) {

}

func (p *rankDaily) Expire(cycle int64) (s, e int64) {
	st := times.Daily(0)
	s = st.Now().Unix()
	et, _ := times.Expire(times.ExpireTypeDaily, 1)
	e = et.Now().Unix()
	return
}

type rankArena struct{}

func (p *rankArena) Truce() int64 {
	arena3V3ResultTime := config.Data.Base["arena3V3ResultTime"]
	return int64(arena3V3ResultTime.Val) * 86400
}
func (p *rankArena) Cycle(skip int64) int64 {
	arena3V3SeasonStart := config.Data.Base["arena3V3SeasonStart"]
	if arena3V3SeasonStart == nil {
		logger.Fatal("config Base arena3V3SeasonStart not exist")
		return 0
	}
	t, err := times.Parse(arena3V3SeasonStart.StrVal)
	if err != nil {
		logger.Fatal("config Base arena3V3SeasonStart 不是一个有效的时间格式:2006-01-02 15:04:05-0700")
		return 0
	}
	arena3V3RoundTime := config.Data.Base["arena3V3RoundTime"]
	arena3V3ResultTime := config.Data.Base["arena3V3ResultTime"]
	day := int64(arena3V3RoundTime.Val+arena3V3ResultTime.Val) * 86400
	now := time.Now().Unix()
	return utils.Ceil(now-t.Now().Unix(), day) + skip

}

func (p *rankArena) Submit(b *rank.Bucket, cycle int64) {

}

func (p *rankArena) Expire(cycle int64) (s, e int64) {
	arena3V3SeasonStart := config.Data.Base["arena3V3SeasonStart"]
	t, _ := times.Parse(arena3V3SeasonStart.StrVal)
	arena3V3RoundTime := config.Data.Base["arena3V3RoundTime"]
	arena3V3ResultTime := config.Data.Base["arena3V3ResultTime"]
	day := int64(arena3V3RoundTime.Val+arena3V3ResultTime.Val) * 86400
	s = t.Now().Unix() + (cycle-1)*day
	e = s + day
	return
}
